import MiniAdUpdate from '../mini-ad-update/mini-ad-update.vue'
import Pages from '../../pages/pages.vue'
import { getMiniAdList, deleteMiniAd, showMsg } from '../../../vuex/action'

export default {
  name: 'MiniAdList',
  components: {
    'vue-mini-ad-update': MiniAdUpdate,
    'vue-pages': Pages
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  ready() {
    this.getMiniList({})
  },
  vuex: {
    getters: {
      fresh: state => state.miniAdListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetMiniAdList: getMiniAdList,
      xDeleteMiniAd: deleteMiniAd
    }
  },
  methods: {
    deleteMiniAd(id) {
      this.xDeleteMiniAd(id)
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    getMiniList({start=this.pages.start, limit=this.pages.limit}) {
      this.xGetMiniAdList({
        start: start,
        limit: limit
      })
        .then((data) => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.totalCount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getMiniList({})
        console.log('a')
      }
    }
  }
}
